﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;

namespace CoolCode.Reflection
{
    public class AssemblyFactory
    {
        public static T CreateInstance<T>(string typeName, string assemblyFile)
        {
            T instance;  
            Assembly assembly = Assembly.LoadFrom(assemblyFile);
            instance = (T)assembly.CreateInstance(typeName);
            return instance;
        }

        /*
        public static T CreateInstance<T>(string typeName, string assemblyName)
        {
            T instance;
            string rootPath = GetRootPath();
            string assemblyFile = Path.Combine(rootPath, assemblyName);
            Assembly assembly = Assembly.LoadFrom(assemblyFile);
            instance = (T)assembly.CreateInstance(typeName);
            return instance;
        }

        private static string _rootPath = string.Empty;

        internal static string GetRootPath()
        {           
            if (string.IsNullOrEmpty(_rootPath))
            {
                //if web application
                //rootPath =  HttpRuntime.BinDirectory;
                _rootPath = typeof(AssemblyFactory).Assembly.Location.GetParentPath(); //Directory.GetCurrentDirectory();
                
            }
            return _rootPath;
        }

        public static void SetBinDirectory(string path)
        {
            _rootPath = path;
        }
        */
    }
}
